package com.zjdiepu.www.biz.base;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.base.DpBaseContractVersionMapper;
import com.zjdiepu.www.ibiz.base.IDpBaseContractVersionService;
import com.zjdiepu.www.model.base.dto.DpBaseContractVersion;
import com.zjdiepu.www.model.base.ext.DpBaseContractVersionExt;

/**
 * 合同版本管理Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年04月13日 11时13分<br/>
 * @since 1.0<br/>
 */
@Service
public class DpBaseContractVersionServiceImpl extends BaseService implements IDpBaseContractVersionService {
	public static final Logger logger = LoggerFactory.getLogger(DpBaseContractVersionServiceImpl.class);

	@Autowired
	private DpBaseContractVersionMapper dpBaseContractVersionMapper;
	
	@Override
	public void saveUpdateAndDelete(DpBaseContractVersion dpBaseContractVersion, OperateType operType) {
		switch (operType) {
		case add:
			dpBaseContractVersion.initInsertData(dpBaseContractVersion.getModifier(), dpBaseContractVersion.getModifierName());
			dpBaseContractVersionMapper.insert(dpBaseContractVersion);
			break;
		case edit:
			dpBaseContractVersion.initUpdateData(dpBaseContractVersion.getModifier(), dpBaseContractVersion.getModifierName());
			// 影响的记录数
			int updateCount = dpBaseContractVersionMapper.updateByPrimaryKeySelective(dpBaseContractVersion);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dpBaseContractVersion.initDeleteData(dpBaseContractVersion.getModifier(), dpBaseContractVersion.getModifierName());
			// 影响的记录数
			int deleteCount = dpBaseContractVersionMapper.updateByPrimaryKeySelective(dpBaseContractVersion);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DpBaseContractVersion> selectByExt(DpBaseContractVersionExt dpBaseContractVersionExt) {
		return dpBaseContractVersionMapper.selectByExt(dpBaseContractVersionExt);
	}
	
	@Override
	public int countByExt(DpBaseContractVersionExt dpBaseContractVersionExt){
		return dpBaseContractVersionMapper.countByExt(dpBaseContractVersionExt);
	}
	
	@Override
	public DpBaseContractVersion selectById(String id) {
		return dpBaseContractVersionMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dpBaseContractVersionMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DpBaseContractVersion dpBaseContractVersion) {
		return dpBaseContractVersionMapper.countRefined(dpBaseContractVersion);
	}

	@Override
	public DpBaseContractVersion selectRefined(DpBaseContractVersion dpBaseContractVersion) {
		return dpBaseContractVersionMapper.selectRefined(dpBaseContractVersion);
	}

	@Override
	public boolean repeat(DpBaseContractVersion dpBaseContractVersion) {
		int count = countRefined(dpBaseContractVersion);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dpBaseContractVersion.getId())) {
			return true;
		}
		DpBaseContractVersion dbItem = selectRefined(dpBaseContractVersion);
		if(null == dbItem) {
			return false;
		}
		return !dpBaseContractVersion.getId().equals(dbItem.getId());
	}
}
